package com.yswy.common.utils;



import java.math.BigDecimal;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Description 数据库工具类
 **/
public class DataBaseUtil {

    private static String SEPARATOR_OF_MAC = ":";

    /**
     * 创建数据库ID (32位)
     *
     * @return
     */
    public static String createId() {
        return byStrMd5(UUID.randomUUID().toString());
    }

    /**
     * 创建数据库编码（参数+14位时间+4位随机）
     *
     * @param head 编码头
     * @return
     */
    public static String createNo(String head) {
        return head + new Date().getTime() + UUIDGenrateUtil.generateShortUuid().substring(0,5);//.buildRandom(4);
    }

    /**
     * 生成纯数字单号
     *
     * @param length 生成长度 = 6位日期 + 1位服务编号 + 的随机数
     * @return 180617100000355507780
     * @Author kangdong
     */
    public static String createNum(int length) {
        int machineId = 1;//最大支持1-9个集群机器部署
        int hashCodeV = UUID.randomUUID().toString().hashCode();
        if (hashCodeV < 0) {//有可能是负数
            hashCodeV = -hashCodeV;
        }
        // 0 代表前面补充0
        // 14 代表长度为14
        // d 代表参数为正数型
        length -= 7;//生成随机数长度
        String date = new SimpleDateFormat("yyMMddHHmmss").format(new Date());
        return date + machineId + String.format("%0" + length + "d", hashCodeV);
    }

    /**
     * 生成日期  精确日期到秒
     */
    public static String createDate() {
        String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        return date;
    }



    public static String createTimes() {
        long timeMillis = System.currentTimeMillis() / 1000;
        return String.valueOf(timeMillis);
    }

    /**
     * 获得指定数目的UUID
     * @param number int 需要获得的UUID数量
     * @return String[] UUID数组
     */
    public static String[] getUUID(int number){
        if(number < 1){
            return null;
        }
        String[] retArray = new String[number];
        for(int i=0;i<number;i++){
            retArray[i] = getUUID();
        }
        return retArray;
    }

    /**
     * //去掉“-”符号
     * @return String UUID
     */
    public static String getUUID(){
        String uuid = UUID.randomUUID().toString();
        //去掉“-”符号
        return uuid.replaceAll("-", "");
    }


    /**
     * @return String UUID
     */
    public static String getUU_ID(){
        return UUID.randomUUID().toString();
    }


    /**
     * MD5加密字符串
     * **/
    public static String byStrMd5(String src) {
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(src.getBytes());
            return to16(md5.digest());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将字节byte转换为16进制字符串输出
     * */
    public static String to16(byte [] b) {
        StringBuffer result = new StringBuffer();
        for(int i = 0;i<b.length;i++) {
            if((b[i] & 0xff)<0x10) {
                result.append("0");
            }
            result.append(Long.toString(b[i] & 0xff, 16));
        }
        return result.toString().toUpperCase();
    }

    /**
     * Generate a random MAC address for qemu/kvm
     * 52-54-00 used by qemu/kvm
     * The remaining 3 fields are random,  range from 0 to 255
     *
     * @return MAC address string
     */
    public static String randomMac4Qemu() {
        Random random = new Random();
        String[] mac = {
                String.format("%02x", 0x52),
                String.format("%02x", 0x54),
                String.format("%02x", 0x00),
                String.format("%02x", random.nextInt(0xff)),
                String.format("%02x", random.nextInt(0xff)),
                String.format("%02x", random.nextInt(0xff))
        };
        return String.join(SEPARATOR_OF_MAC, mac);
    }

    /**
     * 获取下一个编码0001、0002
     * @author kangdong
     * @date 2019/3/27
     * @param num
     * @return java.lang.String
     */
    public static String getNumber(int num) {
        DecimalFormat df = new DecimalFormat("0000");
        num++;
        return df.format(num);
    }


    /**
     * 获取int集合中缺少的元素
     * @author kangdong
     * @date 2019/4/15
     * @return 2,6
     */
    public static List<Integer> lackNum(List<Integer> num) {
        //集合转数组
        Integer[] a = num.toArray(new Integer[num.size()]);
        int b[] = null;
        b = new int[Collections.max(num)];

        for (int i = 0; i < a.length; i++) {
            b[a[i] - 1] = 1;
        }
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < b.length; i++) {
            if (b[i] == 0) {
                list.add(i + 1);
            }
        }
        return list;
    }


    /**
     * 参数为随机数字范围
     * @Author baoruizhe
     * @Date 11:37 2019/12/28
     * @Param [area]
     * @return int
     **/
    public static int randomNumber(int max ,  int min){
        if (max <= 0){
            return 0;
        }
        return (int) (min + Math.random() * (max - min + 1));
    }
    public static long randomNumber(long max ,  long min){
        if (max <= 0){
            return 0;
        }
        return (long) (min + Math.random() * (max - min + 1));
    }
    /**
     * 随机生成指定长度的数字组合，返回字符串
     * @Author baoruizhe
     * @Date 16:04 2020/5/29
     * @Param [length]
     * @return java.lang.String
     **/
    public static String randomNumByLength(int length) {
        StringBuilder randomNumByLength = new StringBuilder();
        Random r = new Random();
        for (int i = 0; i < length; i++) {
            randomNumByLength.append(r.nextInt(9));
        }
        return randomNumByLength.toString();
    }



}
